package p2p.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import p2p.tracker.Tracker;

//import org.jgraph.JGraph;

public class UITrackerBuilder {

	private Tracker tracker;
	private JFrame jframe;
	private JTextArea logging;
	private JButton start;
	private JButton stop;
	private JButton graphButton;

	public UITrackerBuilder(Tracker tracker, JFrame frame, JTextArea logging) {

		this.tracker = tracker;
		this.jframe = frame;
		this.logging = logging;
	}

	public void build() {

		this.initLogging();
		this.initTab();
		this.initToolbar();

	}

	private void initTab() {

		//JTabbedPane tabbedPane = new JTabbedPane();
		// JComponent panel1 = new JGraph();
		//
		// tabbedPane.addTab("Graph-Visualisation", null, panel1,
		// "Graph-Visualisation");
		// tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		//
		// JComponent panel2 = new JTextArea("");
		// tabbedPane.addTab("Graph - Text", null, panel2,
		// "Does twice as much nothing");
		// tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

		//jframe.getContentPane().add(tabbedPane, SwingConstants.CENTER);

	}

	private void initToolbar() {
		JToolBar control = new JToolBar("Control", SwingConstants.HORIZONTAL);

		start = new JButton();
		URL iconUrl = getClass().getResource("start.png");
		ImageIcon icon = new ImageIcon(iconUrl);
		start.setIcon(icon);
		stop = new JButton("");
		iconUrl = getClass().getResource("stop.png");
		icon = new ImageIcon(iconUrl);
		stop.setIcon(icon);
		
		graphButton = new JButton("Draw Graph");

		control.setRollover(true);
		control.add(start);
		control.add(stop);
		control.add(graphButton);
		control.setVisible(true);

		Container container = jframe.getContentPane();
		JPanel toolbarPanel = new JPanel();
		toolbarPanel.add(control);
		container.add(toolbarPanel, BorderLayout.NORTH);

	}

	private void initLogging() {

		logging.setEditable(true);
		logging.setBackground(Color.white);
		JScrollPane scrollPane = new JScrollPane(logging);
		jframe.getContentPane().add(scrollPane, BorderLayout.CENTER);

	}

	public void addActionListener(int tag, ActionListener actionListener) {

		switch (tag) {
		case 0:
			start.addActionListener(actionListener);
			break;
		case 1:
			stop.addActionListener(actionListener);
			break;
		case 2:
			graphButton.addActionListener(actionListener);
			break;
		}

	}

}
